package club.cearnach.core.util;

import club.cearnach.interfaces.Entity2String;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Iterator;

/**
 * @author 阮胜
 * @date 2018/5/14 16:54
 */
public class StringUtil extends StringUtils {
    /**
     * 将集合转成字符串,并且用分隔符隔开
     * 示例:String actorStr = StringUtil.entityCollection2String(movie.getActors(), "/");
     *
     * @param tCollection
     * @param separator
     * @param <T>
     * @return
     */
    public static <T extends Entity2String> String entityCollection2String(Collection<T> tCollection, String separator) {
        Iterator<T> iterator;
        if (CollectionUtils.isEmpty(tCollection) || !(iterator = tCollection.iterator()).hasNext()) {
            return null;
        }
        StringBuilder sb = new StringBuilder(tCollection.size()).append(iterator.next().getName());
        while (iterator.hasNext()) {
            sb.append(separator);
            sb.append(iterator.next().getName());
        }
        return sb.toString();
    }

}
